﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using log4net;

namespace NhJump.Persistence.Context.HttpContextBinding
{
    public class HttpContextUnitOfWorkBinder : IUnitOfWorkBinder
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof(HttpContextUnitOfWorkBinder));
        private static readonly string Key = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName + ".Current";

        public UnitOfWork GetCurrent()
        {
            Logger.DebugFormat("Getting current unit of work for: {0}", Key);
            return HttpContext.Current.Items[Key] as UnitOfWork;
        }

        public void Bind(UnitOfWork unitOfWork)
        {
            Logger.DebugFormat("Binding unit of work for: {0}", Key);
            HttpContext.Current.Items[Key] = unitOfWork;
        }

        public void UnBind()
        {
            Logger.DebugFormat("Unbinding unit of work for: {0}", Key);
            HttpContext.Current.Items.Remove(Key);
        }
    }
}